package com.haiqi.modules.ship.web;

import com.github.pagehelper.PageInfo;
import com.haiqi.common.BaseController;
import com.haiqi.common.annotation.SafeAuthority;
import com.haiqi.common.bean.PageBean;
import com.haiqi.common.persistence.entity.ShipInfo;
import com.haiqi.common.persistence.entity.ShipPositionInfo;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.modules.ship.service.AisShipDynaService;
import com.haiqi.modules.ship.service.ShipPositionInfoService;
import com.haiqi.modules.ship.service.ShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hythzx on 2016/5/25.
 */
@Controller
@RequestMapping("/manage/ship")
@SafeAuthority(menuCode = "fleetManage,monitorLocation")
public class ShipController extends BaseController {

    private String menuCode = "fleetManage";

    @Autowired
    private ShipService shipService;
    @Autowired
    private AisShipDynaService aisShipDynaService;
    @Autowired
    private ShipPositionInfoService shipPositionInfoService;

    @RequestMapping("/list")
    public String list(ShipInfo shipInfo, PageBean pageBean, HttpSession session, Model model) {
        ResponseBean.UserBean user = getUser(session);
        shipInfo.setCompany(user.getOfficeid());
        generateMenuCode(session, menuCode);
        PageInfo<ShipInfo> pageInfo = shipService.getDataList(shipInfo, pageBean);
        model.addAttribute("param", shipInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "/modules/ship/shipList";
    }

    @RequestMapping("/ajax/list")
    @ResponseBody
    public List<ShipInfo> getJsonList(ShipInfo shipInfo, HttpSession session) {
        ResponseBean.UserBean user = getUser(session);
        shipInfo.setCompany(user.getOfficeid());
        List<ShipInfo> shipInfoList = shipService.getDataList(shipInfo);
        return shipInfoList;
    }

    @RequestMapping("/ajax/msaList")
    @ResponseBody
    public List<ShipInfo> getMsaJsonList(ShipInfo shipInfo, HttpSession session) {
        List<ShipInfo> shipInfoList = shipService.getDataList(shipInfo);
        return shipInfoList;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public ShipInfo getShipInfo(String shipid) {
        ShipInfo shipInfo = shipService.getShipInfo(shipid);
        return shipInfo;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map updateShipInfo(ShipInfo shipInfo, HttpSession session) {
        ResponseBean.UserBean user = getUser(session);
        shipInfo.setCompany(user.getOfficeid());
        shipInfo.setShipClass(user.getName());
        Map<String, Boolean> response = new HashMap<>();
        boolean success = shipService.update(shipInfo);
        response.put("success", success);
        return response;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(String shipid) {
        Map<String, Boolean> response = new HashMap<>();
        boolean success = shipService.delete(shipid);
        response.put("success", success);
        return response;
    }

    /**
     *
     * @return
     */
    @RequestMapping("/selectShip")
    @ResponseBody
    public ShipInfo selectShip() {
        ShipInfo shipInfo = aisShipDynaService.selectShip();
        if(shipInfo != null) {
            return shipInfo;
        }
       System.out.println("数据为空");
        return null;
    }

    @RequestMapping("/findByImei")
    @ResponseBody
    public List<ShipPositionInfo> findByImei(String shipimei){
        String[] str = shipimei.split(",");
        List<ShipPositionInfo> list = shipPositionInfoService.findByImei(str);
        if(list != null){
            return list;
        }
        System.out.println("数据为空");
        return null;
    }
}
